import con from './const.js'
export default{
	login(data,store){
		if(data.username===''){
			data.is1=false
			uni.showToast({
			    'title':'请输入用户名'
			});
		}
		else
			data.is1=true
		if(data.password===''){
			data.is2=false
			uni.showToast({
			    'title':'请输入密码'
			});
		}
		else
			data.is2=true
		if(data.is2&&data.is1){
			uni.request({
				url : con.request_ip+'/login/logining',
				method: 'POST',
				sslVerify: false,
				data:{
					"username": data.username,
					"password": data.password
				},
				success: function(res){
					if(res.data.code===200){
						uni.setStorage({
						    key: 'deng',
						    data: res.header,
							success: (res)=>{
								store.commit('login')
							}
						});
						uni.setStorage({
						    key: 'user',
						    data: data.username,
							success: (res)=>{
								store.commit('refresh')
							}
						});
						uni.showToast({
						    'title':'登录成功'
						});
						uni.switchTab({
							url:'/pages/user/user'
						})
					}
					else
						uni.showToast({
							'title': res.data.msg,
						});
				},
				fail: (res) => {
					uni.showToast({
					    'title': '请求发起失败',
					});
				},
				complete:()=>{
					uni.hideLoading();
				}
			})
		}
	},
	register(data,store){
		if(data.password!==data.password2)
			uni.showToast({
			    'title': '两次输入密码不相符',
			});
		else{
			uni.request({
				url : con.request_ip+'/login/registering',
				method: 'POST',
				sslVerify: false,
				data:{
					"username": data.username,
					"password": data.password,
					"password2": data.password2,
					"email": data.mail,
					"yzm": data.yzm
				},
				success: function(res){
					if(res.data.code===200){
						uni.setStorage({
						    key: 'deng',
						    data: res.header,
							success: (res)=>{
								store.commit('login')
							}
						});
						console.log(res.header)
						uni.setStorage({
						    key: 'user',
						    data: data.username,
							success: (res)=>{
								store.commit('refresh')
							}
						});
						uni.showToast({
						    'title':'注册成功'
						});
						uni.switchTab({
							url:'/pages/user/user'
						})
					}
					else
						uni.showToast({
						    'title': res.data.msg,
						});
				},
				fail: (res) => {
					uni.showToast({
					    'title': '请求发起失败',
					});
				},
				complete:()=>{
					uni.hideLoading();
				}
			})
		}
	}
}